<template>
  <div v-show="dialogValue" class="user1-add f-c">
    <BackPage title="帮办风采编辑、添加" show-back @back="onPageBack" />
    <div class="user1-add-box f-g">
      <el-form ref="formRef" :model="ruleForm" :rules="rules" label-position="top" class="user-add-form">
        <el-form-item label="标题" prop="title">
          <el-input clearable v-model="ruleForm.title" style="width: 658px;" />
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input clearable v-model="ruleForm.source" style="width: 658px;" />
        </el-form-item>
        <el-form-item label="发布时间" prop="data">
          <el-date-picker v-model="ruleForm.data" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 658px;" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Rich v-model="ruleForm.content" />
        </el-form-item>
        <el-form-item label="图片" prop="file">
          <DragUpload :url="ruleForm.file" @on-success="url => ruleForm.file = url" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input clearable v-model="ruleForm.sort" type="number" />
        </el-form-item>
        <el-form-item label="显示状态">
          <el-switch v-model="ruleForm.status" :active-value="0" :inactive-value="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave">保存</el-button>
          <el-button @click="dialogValue = false">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { http_elegant } from '@/api'
import dayjs from 'dayjs'
export default {
  name: 'User1Add',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
        id: undefined,
        title: '',
        source: '',
        data: '',
        content: '',
        file: '',
        sort: '',
        status: 0
      },
      rules: {
        title: [{ required: true, message: '不能为空' }],
        source: [{ required: true, message: '不能为空' }],
        data: [{ required: true, message: '不能为空' }],
        file: [{ required: true, message: '不能为空' }]
      }
    }
  },
  methods: {
    async open(id) {
      if (id) {
        const res = await http_elegant.read({ id })
        for (const key in this.ruleForm) {
          this.ruleForm[key] = res.data[key]
        }
      } else {
        this.ruleForm = {
          id: undefined,
          title: '',
          source: '',
          data: dayjs().format('YYYY-MM-DD hh:mm:ss'),
          content: '',
          file: '',
          sort: 0,
          status: 0
        }
      }
      this.dialogValue = true
    },
    onPageBack() {
      this.$emit('get-page')
      this.dialogValue = false
    },
    onSave() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          http_elegant[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm }).then(res => {
            this.$message.success(res.msg)
            this.onPageBack()
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.user1-add {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9;
  background-color: #F6F6F6;
}
.user1-add-box {
  width: 100%;
  background: #FFFFFF;
  border: 1px solid rgba(185, 185, 185, .3);
  border-radius: 16px;
  padding: 60px;
}
</style>
